package com.feng.demo.xstream;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class SingleValueCalendarConverter implements Converter {

	@Override
	public boolean canConvert(Class type) {
		return type.equals(Date.class);
	}

	@Override
	public void marshal(Object source, HierarchicalStreamWriter writer,
			MarshallingContext context) {
		Date date = (Date) source;
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-dd");
        writer.setValue(String.valueOf(formater.format(date))); 

	}

	@Override
	public Object unmarshal(HierarchicalStreamReader reader, 
            UnmarshallingContext context) { 
        Date date = new Date(); 
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-mm-dd");
        
        date = new Date(Long.parseLong(reader.getValue())); 
        return date; 
    }

}
